package com.example.wanandroid.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wanandroid.DataType;
import com.example.wanandroid.R;
import com.example.wanandroid.core.bean.BaseData;
import com.example.wanandroid.core.bean.main.banner.BannerData;
import com.example.wanandroid.core.bean.main.banner.BannerListData;
import com.example.wanandroid.core.bean.main.collect.FeedArticleData;
import com.example.wanandroid.ui.activity.ProjectDetailActivity;
import com.example.wanandroid.ui.widget.ArticleItem;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.transformer.RotateYTransformer;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jiangyi
 */
public class ArticleListAdapter extends RecyclerView.Adapter<ArticleListAdapter.ArticleListViewHolder> {
    private String TAG = "ArticleListAdapter";
    List<BaseData> dataList = new ArrayList<>();
    Context context;
    private LayoutInflater mLayoutInflater;
    Object lock = new Object();


    public ArticleListAdapter(Context context){
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getItemViewType(int position) {
        return dataList.get(position).getViewType();
    }

    @NonNull
    @Override
    public ArticleListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType){
            case DataType.BANNER:
                Banner<BannerData, BannerImageAdapter> banner = (Banner<BannerData, BannerImageAdapter>)mLayoutInflater
                        .inflate(R.layout.head_banner,parent,false);
                return new ArticleListViewHolder(banner);
            case DataType.FEEDARTICLE:
                return new ArticleListViewHolder(mLayoutInflater
                        .inflate(R.layout.article_item,parent,false));
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull ArticleListViewHolder holder, int position) {
        BaseData data = dataList.get(position);
        View itemView = holder.itemView;
        String url;
        switch (data.getViewType()){
            case DataType.BANNER:
                if(itemView instanceof Banner){
                    List<BannerData> bannerList = ((BannerListData)data).getBannerDataList();
                    BannerImageAdapter adapter = new BannerImageAdapter(context,bannerList);
                    ((Banner<BannerData, BannerImageAdapter>) itemView).setAdapter(adapter)
                            .setCurrentItem(bannerList.size(),false)
                            .addPageTransformer(new RotateYTransformer())
                            .setIndicator(new CircleIndicator(itemView.getContext()));
                }
                break;
            case DataType.FEEDARTICLE:
                if(itemView instanceof ArticleItem){
                    ((ArticleItem)itemView).bindData((FeedArticleData) data);
                    url = ((FeedArticleData)data).getLink();
                    holder.card.setOnClickListener(v -> {
                        Intent intent = new Intent(itemView.getContext(), ProjectDetailActivity.class);
                        intent.putExtra("link",url);
                        itemView.getContext().startActivity(intent);
                        Log.d(TAG, "onClick: "+url);
                    });
                }
            default:
                break;
        }

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class ArticleListViewHolder extends RecyclerView.ViewHolder{

        CardView card;

        public ArticleListViewHolder(@NonNull View itemView) {
            super(itemView);
            if(itemView instanceof ArticleItem) {
                card = itemView.findViewById(R.id.article_item_card);
            }
        }
    }

    public void refreshData(List<BaseData> list){
        synchronized (lock){
            this.dataList = list;
            notifyDataSetChanged();
        }
    }

    public void addData(BaseData data){
        synchronized (lock){
            this.dataList.add(data);
            List<BaseData> list = this.dataList.stream().distinct().collect(Collectors.toList());
            this.dataList = list;
            notifyItemInserted(this.dataList.size()-1);
        }
    }

    public void addDataList(List<BaseData> data){
        synchronized (lock){
            this.dataList.addAll(data);
            List<BaseData> list = this.dataList.stream().distinct().collect(Collectors.toList());
            this.dataList = list;
            notifyDataSetChanged();
        }
    }

}
